package com.jxnu.duewang.zmly.zouZhen.question;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Project Zmly
 * Author Duewang
 * Time 2016/12/9 0009
 */

public class CalculateListViewHeight {

    public static void setListViewHeightBasedOnchildren(ListView listView){

        ListAdapter adapter = listView.getAdapter();
        if (adapter == null) {
            return;
        }
        int totalHeight=0;
        for (int i = 0; i < listView.getCount(); i++) {
            View listItem = adapter.getView(i, null, listView);
//            listItem.measure(0,0);//英文是可以的
            int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
            listItem.measure(desiredWidth, 0);
            totalHeight+=listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height=totalHeight+(listView.getDividerHeight()*(adapter.getCount()-1));
        listView.setLayoutParams(params);
        Log.e("测试", String.valueOf(params.height));

    }

}
